﻿using System;
using System.Collections.Generic;
using System.Linq;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Entities;
using WFramework.MvcPager;
using WFramework.ORM;

namespace MiniWeb.Dao
{
    public class ProductsInfoDao : BaseDao<ProductsInfo>
    {
        public PagedList<ProductsInfoDto> GetPagedList(string keyword, int? rid, int? cid, int? pid, bool isSell, bool isChuXiao, bool isHot, bool isWeb, DateTime? starttime, DateTime? endtime,int? pl, int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Append("select ID,ProductsTypeID,ProductsClassID,ProductImg,ProductName,CreateDate ,Price,CPrice,TotalNum,KuCunNum,SellNum,IsSell,IsWeb ,IsHot,IsChuXiao from ProductsInfo with(nolock) ");

            if (rid != null)
            {
                sql.Where("ProductsTypeID=@0", rid);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                sql.Where("ProductsInfo LIKE '@0'", string.Format("%{0}%", keyword));

            }
            if (cid != null)
            {
                sql.Where("ProductsClassID=@0", cid);
            }
            if (pid != null)
            {
                sql.Where("ID =@0", pid);
            }
            if (isHot)
            {
                sql.Where("isHot=@0", isHot);
            }
            if (isSell)
            {
                sql.Where("isSell =@0", isSell);
            }
            if (isChuXiao)
            {
                sql.Where("isChuXiao =@0", true);
            }
            if (isWeb)
            {
                sql.Where("isWeb =@0", isWeb);
            }
            if (starttime != null)
            {
                sql.Where("CreateDate > @0", starttime);
            }
            if (endtime != null)
            {
                sql.Where("CreateDate < @0", endtime);
            }
            if(pl!=null)
            {
                switch (pl)
                {
                    case 1:
                        sql.Where("Price <100");
                        break;
                    case 2:
                        sql.Where("Price > 99");
                        sql.Where("Price <200");
                        break;
                    case 3:
                        sql.Where("Price > 199");
                        sql.Where("Price < 299");
                        break;
                    case 4:
                        sql.Where("Price >299");
                        break;
                }
            }

            sql.OrderBy("CreateDate DESC");
            return PagedList<ProductsInfoDto>(pageIndex, pageSize, sql);
        }


        public List<ChuChuangProductsDto> GetChuChuangList()
        {
            var sql =
                Sql.Builder.Append(
                    @"SELECT wcc.*,pi1.ProductName,pi1.ProductImg,pi1.Price
  FROM WebChuChuang wcc INNER JOIN ProductsInfo pi1
ON wcc.ProductID = pi1.ID
WHERE pi1.IsSell=1");
            return Query<ChuChuangProductsDto>(sql).ToList();
        }
        public ProductsInfo GetById(int id)
        {
            var sql = Sql.Builder.Where("id = @0", id);
            return FirstOrDefault(sql);
        }

        public ProductsInfo GetSellById(int id)
        {
            var sql = Sql.Builder.Where("id = @0 AND IsSell = 1", id);
            return FirstOrDefault(sql);
        }

        public ProductsInfo GetByPosition(int pid)
        {
            var sql = Sql.Builder.Where("Position = @0", pid);
            return FirstOrDefault(sql);
        }
        public void Delete(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            Delete(sql);
        }


        public bool Exists(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            if (FirstOrDefault(sql) != null)
            {
                return true;
            }
            return false;
        }

        public List<ProductsInfoDto> GetAll(int? top, string strwhere, string orderby)
        {
            string sqltop = "";
            if (top != null)
            {
                sqltop = "TOP " + top.ToString() + " ID,ProductImg,ProductName ,CreateDate,Position ";
            }
            else
            {
                sqltop = " ID,ProductImg,ProductName,CreateDate,Position ";
            }
            var sql = Sql.Builder.Select(sqltop).From(" ProductsInfo WITH(NOLOCK)");
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql.Where(strwhere);
            }
            if (!string.IsNullOrEmpty(orderby))
            {
                sql.OrderBy(orderby);
            }
            else
            {
                sql.OrderBy("CreateDate Desc");
            }

            return Query<ProductsInfoDto>(sql).ToList();
        }

        public List<WebProductsInfo> GetWebList()
        {
            var sql =
                Sql.Builder.Append(
                    @"SELECT  TOP  30 ID,ProductName,Price,CPrice,ProductImg,ProductsTypeID FROM ProductsInfo AS T WHERE T.ProductsTypeID IN(1,2,3,5) AND  ID IN(SELECT TOP 10 ID FROM ProductsInfo WHERE ProductsTypeID=T.ProductsTypeID  ORDER BY CreateDate DESC)");
            return Query<WebProductsInfo>(sql).ToList();
        }

        

       


    }
}